﻿using MovingChameLibrary.Models;
using MovingChameLibrary.ServiceManager;
using MovingChameLibrary.TourServiceReference;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;

namespace MovingChameLibrary.ViewModels
{
    public class OfficialTourViewModel : BaseViewModel
    {
        #region Properties
        private double _userLatitude;
        private double _userLongitude;

        private CustomObservableCollection<Tour> _items;
        public CustomObservableCollection<Tour> Items
        {
            get
            {
                if (this._items == null)
                {
                    this._items = new CustomObservableCollection<Tour>();
                }
                return this._items;
            }
            set { this._items = value; }
        }
        #endregion

        #region Constructor
        public OfficialTourViewModel(double userLatitude, double userLongitude)
        {
            this._userLatitude = userLatitude;
            this._userLongitude = userLongitude;
        }
        #endregion

        #region Newest Tours
        private RelayCommand _newestToursCommand;
        public ICommand NewestToursCommand
        {
            get
            {
                if (_newestToursCommand == null)
                {
                    _newestToursCommand = new RelayCommand(new Action(NewestToursExecuted), new Func<bool>(NewestToursCanExecute), false);
                }
                return _newestToursCommand;
            }
        }

        public bool NewestToursCanExecute()
        {
            return true;
        }

        public void NewestToursExecuted()
        {
            ServiceFactory.GetTourServiceClient().GetLatestToursAsync(_userLatitude, _userLongitude, Constants.NumReturnResult);
            ServiceFactory.GetTourServiceClient().GetLatestToursCompleted += OfficialTourViewModel_GetLatestToursCompleted;
        }

        private void OfficialTourViewModel_GetLatestToursCompleted(object sender, TourServiceReference.GetLatestToursCompletedEventArgs e)
        {
            Items.Clear();
            foreach (TourData tourData in e.Result)
            {
                Items.Add(DataConverter.ConvertToTour(tourData, null));
            }
        }
        #endregion

        #region Hottest Tours
        private RelayCommand _hottestToursCommand;
        public ICommand HottestToursCommand
        {
            get
            {
                if (_hottestToursCommand == null)
                {
                    _hottestToursCommand = new RelayCommand(new Action(HottestToursExecuted), new Func<bool>(HottestToursCanExecute), false);
                }
                return _hottestToursCommand;
            }
        }

        public bool HottestToursCanExecute()
        {
            return true;
        }

        public void HottestToursExecuted()
        {
            ServiceFactory.GetTourServiceClient().GetHottestToursAsync(_userLatitude, _userLongitude, Constants.NumReturnResult);
            ServiceFactory.GetTourServiceClient().GetHottestToursCompleted += OfficialTourViewModel_GetHottestToursCompleted;
        }

        private void OfficialTourViewModel_GetHottestToursCompleted(object sender, GetHottestToursCompletedEventArgs e)
        {
            Items.Clear();
            foreach (TourData tourData in e.Result)
            {
                Items.Add(DataConverter.ConvertToTour(tourData, null));
            }
        }
        #endregion

        #region Cheapest Tours
        private RelayCommand _cheapestToursCommand;
        public ICommand CheapestToursCommand
        {
            get
            {
                if (_cheapestToursCommand == null)
                {
                    _cheapestToursCommand = new RelayCommand(new Action(CheapestToursExecuted), new Func<bool>(CheapestToursCanExecute), false);
                }
                return _cheapestToursCommand;
            }
        }

        public bool CheapestToursCanExecute()
        {
            return true;
        }

        public void CheapestToursExecuted()
        {
            ServiceFactory.GetTourServiceClient().GetCheapestToursAsync(_userLatitude, _userLongitude, Constants.NumReturnResult);
            ServiceFactory.GetTourServiceClient().GetCheapestToursCompleted += OfficialTourViewModel_GetCheapestToursCompleted;
        }

        private void OfficialTourViewModel_GetCheapestToursCompleted(object sender, GetCheapestToursCompletedEventArgs e)
        {
            Items.Clear();
            foreach (TourData tourData in e.Result)
            {
                Items.Add(DataConverter.ConvertToTour(tourData, null));
            }
        }
        #endregion
    }
}